<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>项目产品评审流程附加信息</title>
    <jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div id="pageInTab" class="pageInTab">
        <div class="searchArea">
            <form id="searchBar">
                <div class="fieldset1 clearFix">
                    <div class="clearFix"></div>
                    <div class="btnsBox">
                        <span id="addBtn" onclick="addBtn();" class="sq-button sq-button-round">产品挂靠</span>
                        <span id="removeBtn" onclick="removeBtn();" class="sq-button sq-button-round">移除挂靠</span>
                        <span id="addPrdDtlLnkCompBtn" onclick="addPrdDtlLnkCompBtn();" class="sq-button sq-button-round">新增挂靠公司</span>
                    </div>
                </div>
            </form>
        </div>
        <div id="grid" class="sq-grid"></div>
    </div>
</div>

<!--新增挂靠公司时，展示新增页面-->
<div id="div_lnk_comp" style="display: none">
    <form id="div_lnk_comp_form">
        <div class="fieldset1 tabel_col tabel_col_s clearFix noBorder">
            <div class="fieldBox col_50">
                <div class="label">公司名称</div>
                <div class="field">
                    <input name="lnkCompNmBk" id="lnkCompNmBk" type="text"
                           class="ui-textbox" value="" validate="{required:true}"> <span
                        class="requiredSymbol">*</span>
                </div>
            </div>
        </div>
        <div class="btnsBox">
            <span id="hintSpan" style="color:#ff0000;display:none">系统未在工商网中查到此公司，请核对公司名称是否正确</span>
        </div>

        <div class="btnsBox">
            <span id="okBtn1" class="sq-button sq-button-round">确定</span>
            <span id="okBtn2" style="display:none;" class="sq-button sq-button-round">继续编辑</span>
            <span id="cancelBtn" class="sq-button sq-button-round">取消</span>
        </div>
    </form>
</div>

<script type="text/javascript">
    var prjId='${prjId}';
    var grid = null;
    var clickBtnNum = 0;
    var isLnkNmModify = 1;
    var prdDtlIds = [];

    $(function () {
        initGrid();//grid
    });

    //初始化列表
    function initGrid(){
        var manager = null;
        var params = {};
        params.prjId = prjId;
        manager =  $("#grid").ligerGrid({
            url: '<%=path%>/project/prjprddtl/initList.do',
            method : 'get',
            parms:params,
            columns: [
                { display: '产品名称', name: 'prdNm', minWidth: 100,
                    render: function (rowdata, rowindex, value, column) {
                        value ==null?"":value;
                        var h = '';
                        h+='<a  href="javascript:prdDetailBtn(';
                        h+="'"+rowdata.id+"',";
                        h+="'"+rowdata.prjPrdId+"'";
                        h+=')">'+value+'</a>'
                        return h;
                    },
                    enabledSort:true},
                { display: '车辆型号', name: 'prdMdl', minWidth: 140,enabledSort:true},
                getPaySchNoColumn(),
                { display: '挂靠公司名称', name: 'lnkCompNm', minWidth: 100 ,
                    render: function (rowdata, rowindex, value, column) {
                        value ==null?"":value;
                        var h = '';
                        if(value){
                            h+='<a href="javascript:detailBtn(';
                            h+="'"+rowdata.lnkCompId+"'";
                            h+=')">'+value+'</a>'
                        }
                        return h;
                    },
                    enabledSort:true},
                { display: '审核状态', name: 'prjLnkCompVO.audStsCdNm', minWidth: 140,enabledSort:true},
                {
                    display: '操作',
                    isSort: false,
                    width: 200,
                    render: function (rowdata, rowindex, value){
                        var h="";
                        if(rowdata.prjLnkCompVO){
                            if(rowdata.prjLnkCompVO.audStsCd==FlowConstant.FLOW_CHECK_STATE_NON){
                                h='<a class="sq-button-hasicon" href="javascript:modifyBtn(';
                                h+="'"+rowdata.id+"',";
                                h+="'"+rowdata.lnkCompId+"',";
                                h+="'"+rowdata.lnkCompNm+"'";
                                h+=')"><i class="sqBtnIco_4"></i>修改</a>';
                            }
                        }
                        return h;
                    }
                }
            ],
            pageSize:100,
            root:'rows',
            record:'total',
            hideLoadButton : true,
            width: '100%',
            height:'100%',
            heightDiff:-2,
            checkbox:true,
            allowUnSelectRow: true,
            alternatingRow: true,
            rownumbers:true,
            enabledEdit: true,
            usePagerOnLessPagesize: true,
        });
        grid = liger.get("grid");
        grid.setHeight(500);
    }

    function getPaySchNoColumn(){
        var column;
        column ={ display: '支付表号', name: 'paySchNo', minWidth: 140,enabledSort:true,
            render: function (rowdata, rowindex, value){
                if(StringUtils.isEmpty(rowdata.paySchNo)){
                    return "";
                }else{
                    return rowdata.paySchNo;
                }
            }
        };
        return column;
    }


    //添加挂靠公司
    function addBtn(){
        var callback = function(){
            grid.reload();
        }
        var selectedRows = grid.getSelectedRows();
        if(selectedRows.length <1){
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
            return;
        }
        var _ids = [];
        var prjId = "";
        for ( var index in selectedRows) {
            prjId = selectedRows[index].prjId
            _ids.push(selectedRows[index].id);
        }
        var ids =	JSON.stringify(_ids);
        $.ligerDialog.open({  title: '添加产品挂靠公司',height: 500,width: 850, url:'<%=path%>/project/prjprddtl/toChooseLnkCompPage.do?ids='+ids+'&&prjId='+prjId,callback:callback});
    }


    function detailBtn(lnkCmpId){
        if(null==lnkCmpId){
            lnkCmpId = "";
        }
        //$.ligerDialog.open({  title: '挂靠公司详情',height: 500,width: 850, url:'<%=path%>/project/prjlnkcomp/toDetailPage.do?id='+lnkCmpId});
        Add_navTab( 'tab_prj_prd_lnk_detail'+lnkCmpId,'挂靠公司详情','<%=path%>/project/prjlnkcomp/toDetailPage.do?id='+lnkCmpId);
    }

    //有拆分后的产品明细，显示拆分后的产品明细，否则显示项目产品
    function prdDetailBtn(dtlId,prjPrdId){
        //查询产品明细数据
        $.ajax({
            type: 'get',
            dataType:'json',
            data:{id:dtlId},
            url: '<%=path%>/project/prjprddtl/getPrjPrdDtlPrdInfoById.do',
            success:function(msg){
                if(msg.success == 1){
                    var detailFlag = true;
                    var prdTypCd = '';
                    Add_navTab( 'tab_prj_prd_dtl_info'+dtlId,'车辆信息详情','<%=path%>/project/prjprddtl/toModifyPrjPrdDtlPrdInfoPage.do?detailFlag='+detailFlag+'&&dtlId='+dtlId+'&&prdTypCd='+prdTypCd);
                }else{
                    var editFlag = true;
                    Add_navTab('tab_prj_prd_info_detail'+prjPrdId,'项目产品详细信息','<%=path%>/project/vehicle/toModifyPage.do?id='+prjPrdId+'&&prjId='+prjId+'&&detailFlag='+editFlag);
                }
            }
        });

    }


    //所选信息
    function removeBtn(){
        var selectedRows = grid.getSelectedRows();
        if(selectedRows.length <1){
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
            return;
        }
        var _ids = [];
        for ( var index in selectedRows) {
            _ids.push(selectedRows[index].id);
        }
        $.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
            if(flag == false)return;
            $.ajax({
                type: 'post',
                dataType:'json',
                data:{ids:JSON.stringify(_ids)},
                url: '<%=path%>/project/prjprddtl/removePrdDtlLnkCompByIds.do',
                success:function(msg){
                    if(msg.success == 1){
                        grid.reload();
                    }else{
                        top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
                    }
                }
            });
        });
    }


    function callback(lnkCompId,lnkCompNm){
        var selectedRows = grid.getSelectedRows();
        var _ids = [];
        var prjId = "";
        for ( var index in selectedRows) {
            prjId = selectedRows[index].prjId
            _ids.push(selectedRows[index].id);
        }
        var prdDtlIds = JSON.stringify(_ids);
        if(lnkCompId && lnkCompNm && prdDtlIds){
            var params = {};
            params.lnkCompId = lnkCompId;//挂靠公司id
            params.lnkCmpNm = lnkCompNm;//挂靠公司名称
            params.prdDtlIds = prdDtlIds;//产品id
            $.ajax({
                type: 'post',
                dataType:'json',
                data:params,
                url: '<%=path%>/project/prjprddtl/modifyPrdLnkComp.do',
                success : function(msg) {
                    if (msg.success == 1) {
                        //parent.grid.reload();
                        parent.$.ligerDialog.close();
                        parent.$(".l-dialog,.l-window-mask")
                            .css("display", "none");
                    } else {
                        top.$.ligerDialog.SQalert({
                            content : msg.info,
                            SQ_type : "error"
                        });
                    }
                }
            });
        }
        grid.reload();
    }


    function upcallback(lnkCompId,lnkCompNm){
        var selectedRows = grid.getSelectedRows();
        var dtlIds = [];
        for ( var index in selectedRows) {
            dtlIds.push(selectedRows[index].id);
        }
        if(lnkCompId && lnkCompNm){
            $.ajax({
                type: 'get',
                dataType:'json',
                async:false,
                data: {lnkCompId: lnkCompId},
                url: '<%=path%>/project/prjprddtl/getByLnkCompId.do',
                success : function(msg) {
                    if (msg.success == 1) {
                        $.each(msg.data,function(index,obj) {
                            prdDtlIds.push(obj.id);
                        });
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                        grid.reload();
                    } else {
                        top.$.ligerDialog.SQalert({
                            content : msg.info,
                            SQ_type : "error"
                        });
                    }
                }
            });
            if(dtlIds.length != 0){
                var _ids = JSON.stringify(dtlIds);
            }else {
                var _ids = JSON.stringify(prdDtlIds);
            }
            var params = {};
            params.lnkCompId = lnkCompId;//挂靠公司id
            params.lnkCmpNm = lnkCompNm;//挂靠公司名称
            params.prdDtlIds = _ids;//产品id
            if (_ids){
                $.ajax({
                    type: 'post',
                    dataType:'json',
                    async:false,
                    data:params,
                    url: '<%=path%>/project/prjprddtl/modifyPrdLnkComp.do',
                    success : function(msg) {
                        if (msg.success == 1) {
                            grid.reload();
                            parent.$.ligerDialog.close();
                            parent.$(".l-dialog,.l-window-mask")
                                .css("display", "none");
                        } else {
                            top.$.ligerDialog.SQalert({
                                content : msg.info,
                                SQ_type : "error"
                            });
                        }
                    }
                });
            }
            grid.reload();
        }
    }


    function addPrdDtlLnkCompBtn() {
        var selectedRows = grid.getSelectedRows();
        if(selectedRows.length <1){
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_MORE_ROW, SQ_type: "warring"});
            return;
        }
        addDialog = $.ligerDialog.SQopen({
            width: 800,
            top: 100,
            title: '新增',
            target: $("#div_lnk_comp")
        });
        ligerForm = $("#div_lnk_comp_form").ligerForm({
            validate: true
        });

        //清空字段值
        resetForm(ligerForm);
    }

    $('#cancelBtn').click(function(){
        $.ligerDialog.hide();
    });
    $('#okBtn1').click(function(){//确定
        isLnkNmModify = 0;
        checkIsOk();
    });
    $('#okBtn2').click(function(){//继续编辑
        isLnkNmModify = 1;
        checkIsOk();
    });

    function resetForm(ligerForm) {
        ligerForm.setData({
            lnkCompNmBk: "",
            orgCode: "",
            bizLicNo: "",
            taxRegNo: ""
        });
    }

    //检查是否存在公司信息
    function checkIsOk() {
        if (!ligerForm.valid()) {
            UIUtils.alertWarning(LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO);
            return false; //验证不通过，不执行以下代码
        }

        var data = ligerForm.getData();
        data["lnkCompNm"] = data.lnkCompNmBk;
        data["orgCode"] = data.orgCode;
        data["bizLicNo"] = data.bizLicNo;
        data["taxRegNo"] = data.taxRegNo;
        data["clickBtnNum"] = clickBtnNum;

        var _url = '<%=path%>/project/prjlnkcomp/getExistComp.do';
        AjaxUtils.ajaxGet(_url, data, function (msg) {
            if(msg.success == 1){

                if(msg.info == 0){
                    sign = 1;
                    if(clickBtnNum < 2 && isLnkNmModify == 0){
                        clickBtnNum++;
                        //top.$.ligerDialog.SQalert({content:"系统未在工商网中查到此公司，继续操作请点击确定", SQ_type: "success"});
                        $('#hintSpan').css('display', 'block');
                        $('#okBtn1').css('display', 'none');
                        $('#okBtn2').css('display', 'inline-block');
                        return ;
                    }
                }

                var _id = msg.id;
                if(_id){
                    top.$.ligerDialog.confirm("挂靠公司已存在,是否跳转修改页面", function (flag) {
                        if(flag == false){
                            $.ligerDialog.hide();//关闭窗口
                            return;
                        }
                        Add_navTab('tab_prj_lnk_comp_add' + _id, '修改挂靠公司', '<%=path%>/project/prjlnkcomp/toModifyPage.do?id=' + _id+"&isOrSubmit=1&fromSrcBiz=QKYW",upcallback);

                        $.ligerDialog.hide();//关闭窗口
                    });
                }else{
                    var param;
                    var timestamp = (new Date()).valueOf();
                   /* var postParam = {};
                    postParam.lnkCompNm = data.lnkCompNm;
                    postParam.isOrSubmit = "1";
                    postParam.fromSrcBiz = "QKYW";*/
                    if(msg.info == 1){
                        //postParam.hsjBasicId = msg.data;
                        //postParam.importDataSource = "HAOSHUJU";
                        // param = "?lnkCompNm=" + data.lnkCompNmBk + "&creditcode=" + msg.data.creditCode + "&entType=" + msg.data.entType
                        //     + "&address=" + msg.data.address + "&opendate=" + msg.data.openFrom + "&busiscope=" + msg.data.operateScope +"&importDataSource=HAOSHUJU&hsjBasicId="+msg.data.id;
                        param = "?lnkCompNm=" + data.lnkCompNmBk +"&importDataSource=HAOSHUJU&hsjBasicId="+msg.data;
                    }else{
                        //postParam.importDataSource = "INNER";
                        param = "?lnkCompNm=" + data.lnkCompNmBk + "&importDataSource=INNER";
                    }
                    param += "&isLnkNmModify=" + isLnkNmModify+"&isOrSubmit=1&fromSrcBiz=QKYW";
                    //postParam.isLnkNmModify = isLnkNmModify;
                    Add_navTab('tab_prj_lnk_comp_add' + timestamp, '新增挂靠公司', '<%=path%>/project/prjlnkcomp/toModifyPage.do' + param,callback);
                    //Add_navTab_post(postParam, '<%=path%>/project/prjlnkcomp/toModifyPage.do', '新增挂靠公司', callback);
                    $.ligerDialog.hide();//关闭窗口
                }
            }else if(msg.success == 0){
                top.$.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
                //frameElement.dialog.close();
            }
        }, true);
    };


    //修改
    function modifyBtn(id,lnkCompId,lnkCompNm){
        if(!lnkCompId)return;
        Add_navTab('tab_prj_lnk_comp_add' + id, '修改挂靠公司', '<%=path%>/project/prjlnkcomp/toModifyPage.do?id=' + lnkCompId+"&isOrSubmit=1&fromSrcBiz=QKYW",upcallback);
    }

</script>

</body>
</html>